/*
 * ProdutosCadastrar.java
 *
 * Created on 12/02/2009, 16:47:14
 * Copyright Jaime Junior
 */
package Adecot.Produtos;

import Adecot.*;
import Controller.ClassificacaoBO;
import Controller.Facade.IceDecorator;
import Controller.ProdutoBO;
import Controller.VariedadeBO;
import Model.Classificacao;
import Model.Produto;
import Model.Variedade;
import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlInputText;
import com.icesoft.faces.component.ext.HtmlOutputText;
import com.icesoft.faces.component.ext.HtmlSelectOneMenu;
import com.icesoft.faces.component.jsfcl.data.DefaultSelectedData;
import com.icesoft.faces.component.jsfcl.data.DefaultSelectionItems;
import com.icesoft.faces.component.jsfcl.data.PopupBean;
import com.icesoft.faces.component.panelpopup.PanelPopup;
import com.icesoft.faces.component.selectinputtext.SelectInputText;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import java.util.List;
import javax.faces.FacesException;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 */
public class ProdutosCadastrar extends AbstractPageBean {
   // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

   private int __placeholder;

   /**
    * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
    * This method is automatically generated, so any user-specified code inserted
    * here is subject to being replaced.</p>
    */
   private void _init() throws Exception {
   }
   private int codProduto;
   private DefaultSelectionItems campoBuscaIncremental = new DefaultSelectionItems();

   public DefaultSelectionItems getCampoBuscaIncremental() {
      return campoBuscaIncremental;
   }

   public void setCampoBuscaIncremental(DefaultSelectionItems campoBuscaIncremental) {
      this.campoBuscaIncremental = campoBuscaIncremental;
   }
   private DefaultSelectedData variedadeSelecionada = new DefaultSelectedData();

   public DefaultSelectedData getVariedadeSelecionada() {
      return variedadeSelecionada;
   }

   public void setVariedadeSelecionada(DefaultSelectedData dsd) {
      this.variedadeSelecionada = dsd;
   }
   private DefaultSelectionItems variedades = new DefaultSelectionItems();

   public DefaultSelectionItems getVariedades() {
      return variedades;
   }

   public void setVariedades(DefaultSelectionItems dsi) {
      this.variedades = dsi;
   }
   private HtmlSelectOneMenu comboVariedade = new HtmlSelectOneMenu();

   public HtmlSelectOneMenu getComboVariedade() {
      return comboVariedade;
   }

   public void setComboVariedade(HtmlSelectOneMenu hsom) {
      this.comboVariedade = hsom;
   }
   private DefaultSelectedData classificacaoSelecionada = new DefaultSelectedData();

   public DefaultSelectedData getClassificacaoSelecionada() {
      return classificacaoSelecionada;
   }

   public void setClassificacaoSelecionada(DefaultSelectedData dsd) {
      this.classificacaoSelecionada = dsd;
   }
   private DefaultSelectionItems classificacoes = new DefaultSelectionItems();

   public DefaultSelectionItems getClassificacoes() {
      return classificacoes;
   }

   public void setClassificacoes(DefaultSelectionItems dsi) {
      this.classificacoes = dsi;
   }
   private HtmlSelectOneMenu comboClassificacao = new HtmlSelectOneMenu();

   public HtmlSelectOneMenu getComboClassificacao() {
      return comboClassificacao;
   }

   public void setComboClassificacao(HtmlSelectOneMenu hsom) {
      this.comboClassificacao = hsom;
   }
   private PopupBean panelPopup1Bean = new PopupBean();

   public PopupBean getPanelPopup1Bean() {
      return panelPopup1Bean;
   }

   public void setPanelPopup1Bean(PopupBean pb) {
      this.panelPopup1Bean = pb;
   }
   private PopupBean panelPopup1Bean1 = new PopupBean();

   public PopupBean getPanelPopup1Bean1() {
      return panelPopup1Bean1;
   }

   public void setPanelPopup1Bean1(PopupBean pb) {
      this.panelPopup1Bean1 = pb;
   }
   private PanelPopup painelAviso = new PanelPopup();

   public PanelPopup getPainelAviso() {
      return painelAviso;
   }

   public void setPainelAviso(PanelPopup pp) {
      this.painelAviso = pp;
   }
   private PanelPopup painelSair = new PanelPopup();

   public PanelPopup getPainelSair() {
      return painelSair;
   }

   public void setPainelSair(PanelPopup pp) {
      this.painelSair = pp;
   }
   private PopupBean panelPopup1Bean2 = new PopupBean();

   public PopupBean getPanelPopup1Bean2() {
      return panelPopup1Bean2;
   }

   public void setPanelPopup1Bean2(PopupBean pb) {
      this.panelPopup1Bean2 = pb;
   }
   private PanelPopup painelConsulta = new PanelPopup();

   public PanelPopup getPainelConsulta() {
      return painelConsulta;
   }

   public void setPainelConsulta(PanelPopup pp) {
      this.painelConsulta = pp;
   }
   private PopupBean panelPopup1Bean3 = new PopupBean();

   public PopupBean getPanelPopup1Bean3() {
      return panelPopup1Bean3;
   }

   public void setPanelPopup1Bean3(PopupBean pb) {
      this.panelPopup1Bean3 = pb;
   }
   private PanelPopup painelMensagem = new PanelPopup();

   public PanelPopup getPainelMensagem() {
      return painelMensagem;
   }

   public void setPainelMensagem(PanelPopup pp) {
      this.painelMensagem = pp;
   }
   private HtmlCommandButton botaoLimpar = new HtmlCommandButton();

   public HtmlCommandButton getBotaoLimpar() {
      return botaoLimpar;
   }

   public void setBotaoLimpar(HtmlCommandButton hcb) {
      this.botaoLimpar = hcb;
   }
   private HtmlCommandButton botaoSim = new HtmlCommandButton();

   public HtmlCommandButton getBotaoSim() {
      return botaoSim;
   }

   public void setBotaoSim(HtmlCommandButton hcb) {
      this.botaoSim = hcb;
   }
   private HtmlCommandButton botaoNao = new HtmlCommandButton();

   public HtmlCommandButton getBotaoNao() {
      return botaoNao;
   }

   public void setBotaoNao(HtmlCommandButton hcb) {
      this.botaoNao = hcb;
   }
   private HtmlCommandButton botaoOkAviso = new HtmlCommandButton();

   public HtmlCommandButton getBotaoOkAviso() {
      return botaoOkAviso;
   }

   public void setBotaoOkAviso(HtmlCommandButton hcb) {
      this.botaoOkAviso = hcb;
   }
   private HtmlCommandButton botaoNaoAviso = new HtmlCommandButton();

   public HtmlCommandButton getBotaoNaoAviso() {
      return botaoNaoAviso;
   }

   public void setBotaoNaoAviso(HtmlCommandButton hcb) {
      this.botaoNaoAviso = hcb;
   }
   private HtmlCommandButton botaoOk = new HtmlCommandButton();

   public HtmlCommandButton getBotaoOk() {
      return botaoOk;
   }

   public void setBotaoOk(HtmlCommandButton hcb) {
      this.botaoOk = hcb;
   }
   private HtmlCommandButton botaoCancelar = new HtmlCommandButton();

   public HtmlCommandButton getBotaoCancelar() {
      return botaoCancelar;
   }

   public void setBotaoCancelar(HtmlCommandButton hcb) {
      this.botaoCancelar = hcb;
   }
   private HtmlOutputText msgTeste = new HtmlOutputText();

   public HtmlOutputText getMsgTeste() {
      return msgTeste;
   }

   public void setMsgTeste(HtmlOutputText hot) {
      this.msgTeste = hot;
   }
   private HtmlOutputText msgErro = new HtmlOutputText();

   public HtmlOutputText getMsgErro() {
      return msgErro;
   }

   public void setMsgErro(HtmlOutputText hot) {
      this.msgErro = hot;
   }
   private HtmlCommandButton botaoOk1 = new HtmlCommandButton();

   public HtmlCommandButton getBotaoOk1() {
      return botaoOk1;
   }

   public void setBotaoOk1(HtmlCommandButton hcb) {
      this.botaoOk1 = hcb;
   }
   private HtmlCommandButton botaoSair = new HtmlCommandButton();

   public HtmlCommandButton getBotaoSair() {
      return botaoSair;
   }

   public void setBotaoSair(HtmlCommandButton hcb) {
      this.botaoSair = hcb;
   }
   private HtmlInputText nome = new HtmlInputText();

   public HtmlInputText getNome() {
      return nome;
   }

   public void setNome(HtmlInputText hit) {
      this.nome = hit;
   }
   private HtmlOutputText msgAviso = new HtmlOutputText();

   public HtmlOutputText getMsgAviso() {
      return msgAviso;
   }

   public void setMsgAviso(HtmlOutputText hot) {
      this.msgAviso = hot;
   }
   private HtmlCommandButton botaoConsultaOk = new HtmlCommandButton();

   public HtmlCommandButton getBotaoConsultaOk() {
      return botaoConsultaOk;
   }

   public void setBotaoConsultaOk(HtmlCommandButton hcb) {
      this.botaoConsultaOk = hcb;
   }
   private HtmlCommandButton botaoConsultaCancelar = new HtmlCommandButton();

   public HtmlCommandButton getBotaoConsultaCancelar() {
      return botaoConsultaCancelar;
   }

   public void setBotaoConsultaCancelar(HtmlCommandButton hcb) {
      this.botaoConsultaCancelar = hcb;
   }
   private HtmlCommandButton botaoSairSim = new HtmlCommandButton();

   public HtmlCommandButton getBotaoSairSim() {
      return botaoSairSim;
   }

   public void setBotaoSairSim(HtmlCommandButton hcb) {
      this.botaoSairSim = hcb;
   }
   private HtmlCommandButton botaoSairNao = new HtmlCommandButton();

   public HtmlCommandButton getBotaoSairNao() {
      return botaoSairNao;
   }

   public void setBotaoSairNao(HtmlCommandButton hcb) {
      this.botaoSairNao = hcb;
   }
   private HtmlCommandButton botaoAvisoSim = new HtmlCommandButton();

   public HtmlCommandButton getBotaoAvisoSim() {
      return botaoAvisoSim;
   }

   public void setBotaoAvisoSim(HtmlCommandButton hcb) {
      this.botaoAvisoSim = hcb;
   }
   private HtmlOutputText msgAvisoResultado = new HtmlOutputText();

   public HtmlOutputText getMsgAvisoResultado() {
      return msgAvisoResultado;
   }

   public void setMsgAvisoResultado(HtmlOutputText hot) {
      this.msgAvisoResultado = hot;
   }
   private HtmlOutputText msgAvisoPergunta = new HtmlOutputText();

   public HtmlOutputText getMsgAvisoPergunta() {
      return msgAvisoPergunta;
   }

   public void setMsgAvisoPergunta(HtmlOutputText hot) {
      this.msgAvisoPergunta = hot;
   }
   private SelectInputText campoBusca = new SelectInputText();

   public SelectInputText getCampoBusca() {
      return campoBusca;
   }

   public void setCampoBusca(SelectInputText sit) {
      this.campoBusca = sit;
   }
   private HtmlOutputText msgMensagemResultado = new HtmlOutputText();

   public HtmlOutputText getMsgMensagemResultado() {
      return msgMensagemResultado;
   }

   public void setMsgMensagemResultado(HtmlOutputText hot) {
      this.msgMensagemResultado = hot;
   }

   // </editor-fold>
   /**
    * <p>Construct a new Page bean instance.</p>
    */
   public ProdutosCadastrar() {
   }

   /**
    * <p>Callback method that is called whenever a page is navigated to,
    * either directly via a URL, or indirectly via page navigation.
    * Customize this method to acquire resources that will be needed
    * for event handlers and lifecycle methods, whether or not this
    * page is performing post back processing.</p>
    *
    * <p>Note that, if the current request is a postback, the property
    * values of the components do <strong>not</strong> represent any
    * values submitted with this request.  Instead, they represent the
    * property values that were saved for this view when it was rendered.</p>
    */
   @Override
   public void init() {
      // Perform initializations inherited from our superclass
      super.init();
      // Perform application initialization that must complete
      // *before* managed components are initialized
      // TODO - add your own initialiation code here

      // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
      // Initialize automatically managed components
      // *Note* - this logic should NOT be modified
      try {
         _init();
      } catch (Exception e) {
         log("ProdutosCadastrar Initialization Failure", e);
         throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
      }
      setVariedades(new IceDecorator(VariedadeBO.class).montaCombo());

      setClassificacoes(new IceDecorator(ClassificacaoBO.class).montaCombo());

      painelSair.setRendered(false);
      painelConsulta.setRendered(false);
      painelAviso.setRendered(false);
      painelMensagem.setRendered(false);
      //botaoConsultar.setDisabled(true);
      limparCampos();

   // </editor-fold>
   // Perform application initialization that must complete
   // *after* managed components are initialized
   // TODO - add your own initialization code here


   }

   /**
    * <p>Callback method that is called after the component tree has been
    * restored, but before any event processing takes place.  This method
    * will <strong>only</strong> be called on a postback request that
    * is processing a form submit.  Customize this method to allocate
    * resources that will be required in your event handlers.</p>
    */
   @Override
   public void preprocess() {
   }

   /**
    * <p>Callback method that is called just before rendering takes place.
    * This method will <strong>only</strong> be called for the page that
    * will actually be rendered (and not, for example, on a page that
    * handled a postback and then navigated to a different page).  Customize
    * this method to allocate resources that will be required for rendering
    * this page.</p>
    */
   @Override
   public void prerender() {
   }

   /**
    * <p>Callback method that is called after rendering is completed for
    * this request, if <code>init()</code> was called (regardless of whether
    * or not this was the page that was actually rendered).  Customize this
    * method to release resources acquired in the <code>init()</code>,
    * <code>preprocess()</code>, or <code>prerender()</code> methods (or
    * acquired during execution of an event handler).</p>
    */
   @Override
   public void destroy() {
   }

   /**
    * <p>Retornar uma referência para o Bean de dados limitados.</p>
    *
    * @retorna uma referência para o bean de dados limitados
    */
   protected ApplicationBean1 getApplicationBean1() {
      return (ApplicationBean1) getBean("ApplicationBean1");
   }

   /**
    * <p>Retornar uma referência para o Bean de dados limitados.</p>
    *
    * @retorna uma referência para o bean de dados limitados
    */
   protected RequestBean1 getRequestBean1() {
      return (RequestBean1) getBean("RequestBean1");
   }

   /**
    * <p>Retornar uma referência para o Bean de dados limitados.</p>
    *
    * @retorna uma referência para o bean de dados limitados
    */
   protected SessionBean1 getSessionBean1() {
      return (SessionBean1) getBean("SessionBean1");
   }

   //ação dos botões do painel de opções
   public String botaoCadastrar_action() {
      try {
         Produto produto = (Produto) getBean("Produto");
         Classificacao classificacao = (Classificacao) new IceDecorator(ClassificacaoBO.class).retornaObjetoCodigo("codClassificacao",
                 Integer.parseInt(comboClassificacao.getValue().toString()));

         Variedade variedade = (Variedade) new IceDecorator(VariedadeBO.class).retornaObjetoCodigo("codVariedade",
                 Integer.parseInt(comboVariedade.getValue().toString()));
         produto.setVariedade(variedade);
         produto.setClassificacao(classificacao);

         boolean cadastrado = false;
         List<Produto> produtos = new IceDecorator(ProdutoBO.class).consultar();
         for (int i = 0; i < produtos.size(); i++) {
            if (produtos.get(i).getClassificacao().getCodClassificacao() == Integer.parseInt(comboClassificacao.getValue().toString()) && produtos.get(i).getVariedade().getCodVariedade() == Integer.parseInt(comboVariedade.getValue().toString())) {
               cadastrado = true;
            }
         }
         if (!cadastrado) {
            new IceDecorator(ProdutoBO.class).salvar(produto);
            msgMensagemResultado.setValue("Operação Efetuada Com Sucesso");
            painelMensagem.setRendered(true);
         } else {
            msgMensagemResultado.setValue("Produto já cadastrado");

            painelMensagem.setRendered(true);
            System.out.println("akiiiii");
         }
         limparCampos();

      } catch (Exception e) {
         msgAvisoResultado.setValue("Erro Ao Efetuar Operação");
         painelMensagem.setRendered(true);
      }

      return null;
   }

   public String botaoSair_action() {
      painelSair.setRendered(true);
      return null;
   } 

   //método para uso dos botões
   public void limparCampos() {
       FacesContext.getCurrentInstance().getExternalContext().getRequestMap().remove("Produto");
       nome.resetValue();      
      comboClassificacao.resetValue();
      comboVariedade.resetValue();
      campoBusca.resetValue();

   }

   public void desabilitarCampos() {
      nome.setDisabled(true);
      comboClassificacao.setDisabled(true);
      comboVariedade.setDisabled(true);
   }

   public void habilitarCampos() {
      nome.setDisabled(false);
      comboClassificacao.setDisabled(false);
      comboVariedade.setDisabled(false);
   }

   public String botaoNaoAviso_action() {
      painelAviso.setRendered(false);
      painelConsulta.setRendered(false);
      return null;
   }

   public String botaoSairSim_action() {
      //return null means stay on the same page
      return "menu";
   }

   public String botaoSairNao_action() {
      painelSair.setRendered(false);
      return null;
   }      

   public String botaoAvisoSim_action() {
      painelAviso.setRendered(false);
      return null;
   }

   public String botaoMensagemOk_action() {
      painelMensagem.setRendered(false);
      return null;
   }

   public String botaoLimpar_action() {
      this.limparCampos();
      return null;
   }

   public String botaoNovo_action() {
      this.limparCampos();
      return null;
   }

   public String botaoAlterar_action() {
      habilitarCampos();
      return null;
   }
}